package com.example.service.impl;

import com.example.dao.ManagerMapper;
import com.example.dao.UserMapper;
import com.example.entity.*;
import com.example.service.ManagerService;
import com.example.utils.MybatisUtil;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.session.SqlSession;

import java.math.BigDecimal;
import java.util.List;

public class ManagerServiceImpl implements ManagerService {
    private final ManagerMapper mapper;
    private final SqlSession sqlSession;

    public ManagerServiceImpl(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
        this.mapper = sqlSession.getMapper(ManagerMapper.class);
    }

    public void commit() {
        sqlSession.commit();
    }

    public void rollback() {
        sqlSession.rollback();
    }

    @Override
    public void clearCache() {
        sqlSession.clearCache();
    }

    @Override
    public boolean auth(String m_account, String m_password, HttpSession session) {
        Manager manager = mapper.getManager(m_account, m_password);
        if(manager == null) return false;
        session.setAttribute("manager", manager);
        return true;
    }

    @Override
    public Manager getManager(String m_account, String m_password) { return mapper.getManager(m_account, m_password); }

//    @Override
//    public int getPassengerCount() { return mapper.getPassengerCount(); }
//
//    @Override
//    public int getFlightCount() { return mapper.getFlightCount(); }
//
//    @Override
//    public int getOrderCount() { return mapper.getOrderCount(); }
//
//    @Override
//    public List<Order> getOrderList() { return mapper.getOrderList(); }

    //订单

    @Override
    public int getOrderCountByDate(String date) {
        return mapper.getOrderCountByDate(date);
    }
    @Override
    public int getCancelledAndRefundOrderNum() {
        return mapper.getCancelledAndRefundOrderNum();
    }
    @Override
    public double getPaidOrderMoneyByDate(String startDate, String endDate) {
        String money = mapper.getPaidOrderMoneyByDate(startDate, endDate);
        if(money == null) return 0;
        return Double.parseDouble(money);
    }
    @Override
    public int getRefundOrderCount() {
        return mapper.getRefundOrderCount();
    }
    @Override
    public int getRefundOrderNumByDate(String startDate, String endDate) {
        String num = mapper.getRefundOrderNumByDate(startDate, endDate);
        if(num == null) return 0;
        return Integer.parseInt(num);
    }
    @Override
    public int getOrderNum() {
        return mapper.getOrderNum();
    }
    @Override
    public int getUnpaidOrderNum() {
        return mapper.getUnpaidOrderNum();
    }
    @Override
    public int getPaidOrderNum() {
        return mapper.getPaidOrderNum();
    }
    @Override
    public int getCompletedOrderNum() {
        return mapper.getCompletedOrderNum();
    }
    @Override
    public int getCancelledOrderNum() {
        return mapper.getCancelledOrderNum();
    }
    @Override
    public List<Order> getOrderList() {
        return mapper.getOrderList();
    }
    //航班
    @Override
    public int getFlightCountByDate(String date) {
        return mapper.getFlightCountByDate(date);
    }
    @Override
    public int getDelayFlightNum(String date) {
        return mapper.getDelayFlightNum(date);
    }
    @Override
    public int getCancelFlightNum(String date) {
        return mapper.getCancelFlightNum(date);
    }
    @Override
    public int getFlightSpaceByDate(String date) {
        String space = mapper.getFlightSpaceByDate(date);
        if(space == null) return 0;
        return Integer.parseInt(space);
    }
    @Override
    public List<FlightWithStatus> getFlightList() {
        return mapper.getFlightList();
    }

    //报表
    @Override
    public BigDecimal getTotalMoney() {
        return mapper.getTotalMoney();
    }
    @Override
    public BigDecimal getTotalMoneyByDate(String startDate, String endDate) {
        BigDecimal totalRevenue = mapper.getTotalMoneyByDate(startDate, endDate);
        return totalRevenue != null ? totalRevenue : BigDecimal.ZERO;
    }
    @Override
    public int getTotalFlightNum() {
        return mapper.getTotalFlightNum();
    }
    @Override
    public int getTotalDelayFlightNum() {
        return mapper.getTotalDelayFlightNum();
    }
    @Override
    public int getTotalFlightNumByDate(String startDate, String endDate) {
        return mapper.getTotalFlightNumByDate(startDate, endDate);
    }
    @Override
    public int getTotalUserNum() {
        return mapper.getTotalUserNum();
    }
    @Override
    public int getTotalUserNumByDate(String startDate, String endDate) {
        return mapper.getTotalUserNumByDate(startDate, endDate);
    }
    @Override
    public List<CityCount> getTop6Cities() {
        return mapper.getTop6Cities();
    }
    @Override
    public List<TopRoute> getTop5RoutesByDate(String startDate, String endDate) {
        return mapper.getTop5RoutesByDate(startDate, endDate);
    }
    @Override
    public int getOrderCountByLongDate(String startDate, String endDate) {
        return mapper.getOrderCountByLongDate(startDate, endDate);
    }
    @Override
    public int getEconomyOrderCountByLongDate(String startDate, String endDate) {
        return mapper.getEconomyOrderCountByLongDate(startDate, endDate);
    }
    @Override
    public int getBusinessOrderCountByLongDate(String startDate, String endDate) {
        return mapper.getBusinessOrderCountByLongDate(startDate, endDate);
    }
    @Override
    public int getFirstOrderCountByLongDate(String startDate, String endDate) {
        return mapper.getFirstOrderCountByLongDate(startDate, endDate);
    }
}
